package com.sys.service;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sys.Entity.DataEntity;
import com.sys.dao.CrudDao;
import com.sys.utils.Page;
public abstract class CrudService <D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {
	@Autowired
	protected D dao;
	public T get(String id) {
		return dao.get(id);
	}
	public T get(T entity) {
		return dao.get(entity);
	}
	public List<T> findList(T entity) {
		return dao.findList(entity);
	}
	public PageInfo<T> findPage2(PageInfo<T> page, T entity) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		page.setList(dao.findList(entity));
		return page;
	}
	public Page<T> findPage(Page<T> page, T entity) {
		entity.setPage(page);
		page.setList(dao.findList(entity));
		return page;
	}
	public Page<T> findPageone(Page<T> page, T entity) {
		entity.setPage(page);
		page.setList(dao.findListone(entity));
		return page;
	}
	@Transactional(readOnly = false)
	public void save(T entity) {
		if (entity.getIsNewRecord()){
			dao.insert(entity);
		}else{
			dao.update(entity);
		}
	}
	@Transactional(readOnly = false)
	public void delete(T entity) {
		dao.delete(entity);
	}
	@Transactional(readOnly = false)
	public void deleteAll(Collection<T> entitys) {
		for (T entity : entitys) {
			dao.delete(entity);
		}
	}
}
